<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-col :span="12">
        <el-form-item label="报告名称" prop="bgmc">
          <el-input v-model="dataForm.bgmc" :disabled="disabled" :maxlength="100" placeholder="报告名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="报告周期" prop="bgzq">
          <el-input v-model="dataForm.bgzq" :disabled="disabled" :maxlength="100" placeholder="报告周期"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="报告类型" prop="bglx">
          <el-dict code="YZB_BGLX" placeholder="报告类型" v-model="dataForm.bglx" :disabled="disabled"></el-dict>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="报告路径" prop="bglj">
          <a v-if="dataForm.bglj" @click="downloadFiles()"
             style="font-size:12px;cursor: pointer">下载</a>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="dataForm.createTime" :disabled="disabled" :maxlength="100" placeholder="创建时间"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="报告内容" prop="bgnr">
          <ueditor v-model="dataForm.bgnr" :height="400" :disabled="disabled"></ueditor>
        </el-form-item>
      </el-col>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {downloadFile} from '@/utils'

export default {
  data() {
    return {
      disabled: false,
      visible: false,
      dataForm: {
        id: '',
        bgmc: '',
        bgzq: '',
        bglx: '',
        bgnr: '',
        bglxMc: '',
        bglj: '',
        isDeleted: '',
        createTime: ''
      },
      dataRule: {
        bgmc: [
          {required: true, message: '报告名称不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init(id, disabled) {
      this.disabled = disabled
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: `/zr/yzb/info/${this.dataForm.id}`,
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.yzb
            }
          })
        }
      })
    },
    downloadFiles() {
      downloadFile(this.dataForm.bglj, this.dataForm.bgmc)
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/zr/yzb/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
    }
  }
}
</script>
